﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;
using BPM.Base.Tools;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using AutoMapper;


namespace BPM.Foundation.PWService
{
    public partial interface IPowerService
    {
        [System.ServiceModel.OperationContract]
        int? delMenuAndPower(Guid menuId);

        [System.ServiceModel.OperationContract]
        List<MenuDto> getMenuSelectedAllByDuty(PW_Duty duty);

        [System.ServiceModel.OperationContract]
        int? saveMenuDutyRelForDuty(PW_Duty duty, List<PW_MenuDutyRelation> rels);


        [System.ServiceModel.OperationContract]
        List<PW_Object> getDutyObjRelsByUserNo(string userNo);

        [System.ServiceModel.OperationContract]
        List<PW_Menu> getMenuByType(int menuType);

        [System.ServiceModel.OperationContract]
        int? createMenuObjects(List<MenuObjectDto> obj);

        [System.ServiceModel.OperationContract]
        List<MenuObjectDto> getMenuObjectAll();

        [System.ServiceModel.OperationContract]
        List<MenuObjectDto> getMenuObjectByMenuId(Guid meunId);

        [System.ServiceModel.OperationContract]
        int? addMenuObject(MenuObjectDto objDto);

        [System.ServiceModel.OperationContract]
        int? updateMenuObject(MenuObjectDto objDto);

        [System.ServiceModel.OperationContract]
        int? delMenuObject(MenuObjectDto objDto);

        [System.ServiceModel.OperationContract]
        int? saveDutyPower(PW_Duty duty, List<PW_MenuDutyRelation> mrels, List<PW_ObjectDutyRelation> addOrels, List<PW_ObjectDutyRelation> delOrels);

        [System.ServiceModel.OperationContract]
        List<MenuObjectDto> getMenuObject(Guid menuId, Guid dutyId);

        [System.ServiceModel.OperationContract]
        List<PW_Object> getMenuObjectByUserNo(Guid menuId, string userNo);


        [System.ServiceModel.OperationContract]
        List<PW_Menu> getFormAll();

    }

    public partial class PowerService : IPowerService
    {
        //[3.1	添加部门]
        public int? insertDepartment(string pid, string parentDepartmentNo, PW_Department department)
        {

            //[1]
            if (department == null)
            {
                return 1;
            }

            //-

            //[2]
            if (string.IsNullOrEmpty(parentDepartmentNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(department.departmentNo))
            {
                return 3;
            }
            //-

            //[4]
            if (string.IsNullOrEmpty(department.departmentName))
            {
                return 4;
            }
            //-

            //[5]
            if (string.IsNullOrEmpty(department.departmentType))
            {
                return 5;
            }
            //-

            //[6]
            string tableName = "PW_Department";
            string filedName = "departmentType";
            List<SYS_Enumeration> ls = this.getEnumeration(pid).Where(p => p.tableName == tableName && p.fieldName == filedName).ToList();
            if (ls == null)
            {
                return 6;
            }
            if (ls.Count == 0)
            {
                return 6;
            }

            if (ls.Where(p => p.value == department.departmentType).Count() == 0)
            {
                return 6;
            }
            //-


            //[7]
            if (PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == parentDepartmentNo) == null)
            {
                return 7;
            }

            //-

            //[8]
            department.ID = Guid.NewGuid();
            //-
            //[9]
            if (department.isFreeze == null)
            {
                department.isFreeze = 0;
            }
            //-

            //[10]
            department.departmentCreateTime = DateTime.Now;
            //-

            //[11]

            if (department.departmentWeight == null || department.departmentWeight < 3)
            {
                department.departmentWeight = 3;
            }
            //-

            //[12]
            department.departmentRating = PWDB.PW_Department.Single(p => p.departmentNo == parentDepartmentNo).departmentRating + 1;
            //-

            //[13]
            if (string.IsNullOrEmpty(department.departmentDescription))
            {
                department.departmentDescription = string.Empty;
            }
            //-
            //[14]
            department.parentDepartment = parentDepartmentNo;
            //-

            //[15]
            if (PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == department.departmentNo) == null)
            {
                this.PWDB.PW_Department.InsertOnSubmit(department);

                this.submit();


                //[16]
                var ce = createExpansionTable(department.departmentNo, "PW_Department");

                if (ce != null)
                {
                    return 9;
                }


                //[17]
                var v = bindDutyDepartment(pid, "officeuser", department.departmentNo);
                if (v != null)
                {
                    return 10;
                }
                //[18]
                //v = bindDutyDepartment(pid, "all", department.departmentNo);
                //v = bindDutyDepartment(pid, "none", department.departmentNo);
                //if (v != null)
                //{
                //    return 11;
                //}


                return null;
            }
            else
            {
                return 8;
            }

            //-
        }
        //-

        //[3.2	修改部门]
        public int? updateDepartment(string pid, PW_Department department)
        {
            if (checkKey(department.departmentNo)) { return -1000; }


            //[1]
            if (department == null)
            {
                return 1;
            }
            //--

            //[2]
            if (department.isFreeze == null)
            {
                return 2;
            }
            //--

            //[3]
            if (string.IsNullOrEmpty(department.departmentNo))
            {
                return 3;
            }
            //--

            //[4]
            if (string.IsNullOrEmpty(department.departmentName))
            {
                return 4;
            }
            //--

            //[5]
            if (string.IsNullOrEmpty(department.departmentType))
            {
                return 5;
            }
            //--

            //[6]
            string tableName = "PW_Department";
            string filedName = "departmentType";
            List<SYS_Enumeration> ls = this.getEnumeration(pid).Where(p => p.tableName == tableName && p.fieldName == filedName).ToList();
            if (ls == null)
            {
                return 6;
            }
            if (ls.Count == 0)
            {
                return 6;
            }
            if (ls.Where(p => p.value == department.departmentType).Count() == 0)
            {
                return 6;
            }
            //-

            //[7]
            if (department.departmentRating == 0)
            {
                return 7;
            }
            //-
            //[8]
            if (department.ID == Guid.Empty)
            {
                return 8;
            }
            //-

            //[9]
            if (PWDB.PW_Department.Count(p => p.ID == department.ID) == 0)
            {
                return 9;
            }
            //-

            //[10]
            if (department.departmentWeight == null || department.departmentWeight < 3)
            {
                return 10;
            }
            //-

            //[11]
            PWDB.PW_Department dp = PWDB.PW_Department.Single(p => p.ID == department.ID);

            if (dp == null)
            {
                return -1;
            }

            if (dp.departmentNo != department.departmentNo)
            {
                return -1;
            }

            if (dp.parentDepartment != department.parentDepartment)
            {
                return -1;
            }

            if (dp.departmentRating != department.departmentRating)
            {
                return -1;
            }

            if (dp.departmentCreateTime != department.departmentCreateTime)
            {
                return -1;
            }

            //-

            //[12]
            dp.departmentDescription = department.departmentDescription;
            dp.departmentName = department.departmentName;
            dp.departmentType = department.departmentType;
            dp.departmentWeight = department.departmentWeight;
            dp.isFreeze = department.isFreeze;
            //-

            return submit();


        }
        //-

        //[3.3	得到部门]
        public PW_Department getDepartment(string pid, string departmentNo)
        {
            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[2]
            var v = PWDB.PW_Department.Count(p => p.departmentNo == departmentNo);
            if (v == 1)
            {

                return PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == departmentNo);
            }
            else
            {
                return null;
            }
            //-
        }
        //-

        //[3.4	查找部门]
        public List<PW_Department> searchDepartment(string pid, PW_Department department, int type)
        {
            //[1]
            if (department == null)
            {
                return PWDB.PW_Department.ToList();
            }


            //[2]
            if (type == 1)
            {
                string departmentDescription = null;
                string departmentType = null;
                string departmentName = null;
                string departmentNo = null;
                if (!string.IsNullOrEmpty(department.departmentDescription))
                {
                    departmentDescription = "%" + department.departmentDescription + "%";
                }
                if (!string.IsNullOrEmpty(department.departmentType))
                {
                    departmentType = "%" + department.departmentType + "%";
                }
                if (!string.IsNullOrEmpty(department.departmentName))
                {
                    departmentName = "%" + department.departmentName + "%";
                }

                if (!string.IsNullOrEmpty(department.departmentNo))
                {
                    departmentNo = "%" + department.departmentNo + "%";
                }



                var ls = PWDB.PW_Department.Where(p => System.Data.Linq.SqlClient.SqlMethods.Like(p.departmentName, departmentName) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.departmentType, departmentType) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.departmentDescription, departmentDescription) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.departmentNo, departmentNo)
                                             );
                return ls.ToList();

            }

            //[3]
            if (type == 2)
            {
                if (department.departmentCreateTime != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentCreateTime == department.departmentCreateTime).ToList();
                }
                if (department.departmentType != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentType == department.departmentType).ToList();
                }
                if (department.departmentDescription != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentDescription == department.departmentDescription).ToList();
                }
                if (department.departmentName != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentName == department.departmentName).ToList();
                }
                if (department.departmentNo != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentNo == department.departmentNo).ToList();
                }
                if (department.departmentWeight != null)
                {
                    return PWDB.PW_Department.Where(p => p.departmentWeight == department.departmentWeight).ToList();
                }
                if (department.isFreeze != null)
                {
                    return PWDB.PW_Department.Where(p => p.isFreeze == department.isFreeze).ToList();
                }
            }

            return PWDB.PW_Department.Where(p => false).ToList();
        }
        //-

        //[3.5	删除部门]
        public int? removeDepartment(string pid, string departmentNo)
        {
            if (checkKey(departmentNo)) { return -1000; }


            //[1]
            if (string.IsNullOrEmpty(departmentNo))
            {
                return 1;
            }
            //-

            //[2]
            if (PWDB.PW_Department.SingleOrDefault(p => p.departmentNo == departmentNo) == null)
            {
                return 2;
            }
            //-

            //[3]

            if (PWDB.PW_Department.Where(p => p.parentDepartment == departmentNo).ToList().Count != 0)
            {
                return 3;
            }
            //-

            //[4]
            PWDB.PW_Department.DeleteOnSubmit(PWDB.PW_Department.Single(p => p.departmentNo == departmentNo));
            return submit();
            //-

        }
        //-

        //[3.6	添加职能]
        public int? insertDuty(string pid, PW_Duty duty)
        {
            //[1]
            if (duty == null)
            {
                return 1;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(duty.dutyNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(duty.dutyName))
            {
                return 3;
            }
            //-

            //[4]
            duty.ID = Guid.NewGuid();
            //-

            //[5]
            if (string.IsNullOrEmpty(duty.dutyDescription))
            {
                duty.dutyDescription = "没填";
            }
            //-

            //[6]
            duty.dutyCreateTime = DateTime.Now;
            //-

            //[7]
            if (duty.isFreeze == null)
            {
                duty.isFreeze = 0;
            }
            //-

            //[8]
            duty.dutyWeight = 1;
            //-

            //[9]
            //-

            //[10]
            if (PWDB.PW_Duty.Count(p => p.dutyNo == duty.dutyNo) == 0)
            {
                PWDB.PW_Duty.InsertOnSubmit(duty);
                this.submit();
                var ce = createExpansionTable(duty.dutyNo, "PW_Duty");

                //[11]
                if (ce != null)
                {
                    return 5;
                }

                return null;
            }
            else
            {
                return 4;
            }
            //-
        }
        //-

        //[3.7	修改职能]
        public int? updateDuty(string pid, PW_Duty duty)
        {
            if (checkKey(duty.dutyNo)) { return -1000; }


            //[1]
            if (duty == null)
            {
                return 1;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(duty.dutyNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(duty.dutyName))
            {
                return 3;
            }
            //-

            //[4]
            if (string.IsNullOrEmpty(duty.dutyDescription))
            {
                duty.dutyDescription = "没填";
            }
            //-

            //[5]
            if (duty.isFreeze == null)
            {
                duty.isFreeze = 0;
            }
            //-

            //[6]
            if (PWDB.PW_Duty.Count(p => p.ID == duty.ID) == 1)
            {
                var pw_duty = PWDB.PW_Duty.Single(p => p.ID == duty.ID);
                pw_duty.dutyName = duty.dutyName;
                pw_duty.dutyCreateTime = duty.dutyCreateTime;
                pw_duty.dutyDescription = duty.dutyDescription;
                pw_duty.isFreeze = duty.isFreeze;
                pw_duty.dutyWeight = duty.dutyWeight;
                pw_duty.parentDuty = duty.parentDuty;
                pw_duty.dutyType = duty.dutyType;
                return submit();
            }
            else
            {
                return 4;
            }
            //-
        }
        //-

        //[3.8	得到职能]
        public PW_Duty getDuty(string pid, string dutyNo)
        {
            //[1]

            if (string.IsNullOrEmpty(dutyNo))
            {
                return null;
            }

            //[2]
            if (PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo) == 1)
            {
                var pw_duty = PWDB.PW_Duty.Single(p => p.dutyNo == dutyNo);
                return pw_duty;
            }
            else
            {
                return null;
            }
            //-
        }

        //-
        public List<PW_Duty> getAllDuty()
        {
            return PWDB.PW_Duty.ToList();
        }

        //-

        //[3.9	查找职能]
        public List<PW_Duty> searchDuty(string pid, PW_Duty duty, int type)
        {
            //[1]
            if (duty == null)
            {
                return PWDB.PW_Duty.ToList();
            }

            //[2]
            if (type == 1)
            {
                string dutyDescription = null;
                string dutyName = null;
                string dutyNo = null;
                if (!string.IsNullOrEmpty(duty.dutyDescription))
                {
                    dutyDescription = "%" + duty.dutyDescription + "%";
                }
                if (!string.IsNullOrEmpty(duty.dutyName))
                {
                    dutyName = "%" + duty.dutyName + "%";
                }

                if (!string.IsNullOrEmpty(duty.dutyNo))
                {
                    dutyNo = "%" + duty.dutyNo + "%";
                }



                var ls = PWDB.PW_Duty.Where(p => System.Data.Linq.SqlClient.SqlMethods.Like(p.dutyName, dutyName) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.dutyDescription, dutyDescription) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.dutyNo, dutyNo)
                                             );

                return ls.ToList();

            }

            //[3]
            if (type == 2)
            {
                if (duty.dutyCreateTime != null)
                {
                    return PWDB.PW_Duty.Where(p => p.dutyCreateTime == duty.dutyCreateTime).ToList();
                }
                if (duty.dutyDescription != null)
                {
                    return PWDB.PW_Duty.Where(p => p.dutyDescription == duty.dutyDescription).ToList();
                }
                if (duty.dutyName != null)
                {
                    return PWDB.PW_Duty.Where(p => p.dutyName == duty.dutyName).ToList();
                }
                if (duty.dutyNo != null)
                {
                    return PWDB.PW_Duty.Where(p => p.dutyNo == duty.dutyNo).ToList();
                }
                if (duty.dutyWeight != null)
                {
                    return PWDB.PW_Duty.Where(p => p.dutyWeight == duty.dutyWeight).ToList();
                }
                if (duty.isFreeze != null)
                {
                    return PWDB.PW_Duty.Where(p => p.isFreeze == duty.isFreeze).ToList();
                }
            }

            return null;
        }
        //-

        //[3.10	删除职能]
        public int? removeDuty(string pid, string dutyNo)
        {

            if (checkKey(dutyNo)) { return -1000; }

            //[1]
            if (string.IsNullOrEmpty(dutyNo))
            {
                return 1;
            }

            //[2]
            if (PWDB.PW_Duty.Count(p => p.dutyNo == dutyNo) == 1)
            {
                var pw_duty = PWDB.PW_Duty.Single(p => p.dutyNo == dutyNo);
                PWDB.PW_Duty.DeleteOnSubmit(pw_duty);
                submit();   //级联删除出错后如何得到返回结果
                return null;
            }
            else
            {
                return 2;
            }
        }
        //-

        //[3.11	添加人员]
        public int? insertUser(string pid, PW_User user)
        {
            //[1]
            if (user == null)
            {
                return 1;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(user.userNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(user.firstName))
            {
                return 3;
            }
            //-

            //[4]
            user.ID = Guid.NewGuid();
            //-



            //[5]
            if (string.IsNullOrEmpty(user.password))
            {
                user.password = this.passwordMD5(user.userNo);
            }
            else
            {
                user.password = this.passwordMD5(user.password);
            }
            //-

            //[6]
            if (string.IsNullOrEmpty(user.sex))
            {
                user.sex = "没填";
            }
            //-

            //[7]
            if (string.IsNullOrEmpty(user.email))
            {
                user.email = "";
            }

            //-

            //[8]
            if (user.isFreeze == null)
            {
                user.isFreeze = 0;
            }
            //-

            //[9]
            if (user.birthDate == null)
            {
                user.birthDate = new DateTime(1900, 1, 1);
            }
            //-

            //[10]
            if (string.IsNullOrEmpty(user.showRule))
            {
                user.showRule = string.Empty;
            }

            if (string.IsNullOrEmpty(user.phone))
            {
                user.phone = string.Empty;
            }

            if (string.IsNullOrEmpty(user.lastName))
            {
                user.lastName = string.Empty;
            }

            if (string.IsNullOrEmpty(user.alias))
            {
                user.alias = string.Empty;
            }
            //-


            //[11]

            if (PWDB.PW_User.Count(p => p.userNo == user.userNo) == 0)
            {

                PWDB.PW_User.InsertOnSubmit(user);

                this.submit();

                var ce = createExpansionTable(user.userNo, "PW_User");

                //[12]
                if (ce != null)
                {
                    return 5;
                }


                return null;
            }
            else
            {
                return 4;
            }
            //-


        }
        //-

        //[3.12	修改人员]
        public int? updateUser(string pid, PW_User user)
        {
            if (checkKey(user.userNo)) { return -1000; }

            //[1]
            if (user == null)
            {
                return 1;
            }
            //-

            //[2]
            if (user.ID == Guid.Empty)
            {
                return 3;
            }
            //-

            //[3]
            if (PWDB.PW_User.SingleOrDefault(p => p.ID == user.ID) == null)
            {
                return 4;
            }
            //-

            //[4]
            if (PWDB.PW_User.SingleOrDefault(p => p.ID == user.ID).userNo != user.userNo)
            {
                return -1;
            }
            //-

            //[5]
            PW_User p1 = PWDB.PW_User.Single(p => p.ID == user.ID);

            //-
            if (!string.IsNullOrEmpty(user.firstName))
            {
                p1.firstName = user.firstName;
            }

            //-
            if (!string.IsNullOrEmpty(user.password))
            {
                p1.password = passwordMD5(user.password);
            }
            else
            {
                p1.password = passwordMD5(user.userNo);
            }

            //-
            if (string.IsNullOrEmpty(user.sex))
            {
                p1.sex = "没填";
            }
            else
            {
                p1.sex = user.sex;
            }



            if (user.alias != null)
            {
                p1.alias = user.alias;
            }

            if (user.birthDate != null)
            {
                p1.birthDate = user.birthDate;
            }

            if (user.lastName != null)
            {
                p1.lastName = user.lastName;
            }

            if (!string.IsNullOrEmpty(user.email))
            {
                p1.email = user.email;
            }

            if (!string.IsNullOrEmpty(user.phone))
            {
                p1.phone = user.phone;
            }

            if (!string.IsNullOrEmpty(user.showRule))
            {
                p1.showRule = user.showRule;
            }

            if (user.isFreeze != null)
            {
                p1.isFreeze = user.isFreeze;
            }


            return submit();
            //-


        }
        //-

        //[3.13	得到人员]
        public PW_User getUser(string pid, string userNo)
        {
            if (string.IsNullOrEmpty(userNo))
            {
                return null;
            }

            if (PWDB.PW_User.Count(p => p.userNo == userNo) == 1)
            {
                PW_User user = PWDB.PW_User.SingleOrDefault(p => p.userNo == userNo);

                return user;
            }
            else
            {
                return null;
            }
        }
        //-

        //[3.14	查找人员]
        public List<PW_User> searchUser(string pid, PW_User user, int type)
        {
            //[1]
            if (user == null)
            {
                return PWDB.PW_User.ToList();
            }


            //[2]
            if (type == 1)
            {
                string userNo = null;
                string password = null;
                string lastName = null;
                string firstName = null;
                string alias = null;
                string sex = null;
                string birthDate = null;
                string phone = null;
                string email = null;
                string showRule = null;

                if (!string.IsNullOrEmpty(user.showRule))
                {
                    showRule = "%" + user.showRule + "%";
                }
                if (!string.IsNullOrEmpty(user.email))
                {
                    email = "%" + user.email + "%";
                }
                if (!string.IsNullOrEmpty(user.phone))
                {
                    phone = "%" + user.phone + "%";
                }
                if (!string.IsNullOrEmpty(user.sex))
                {
                    sex = "%" + user.sex + "%";
                }
                if (!string.IsNullOrEmpty(user.alias))
                {
                    alias = "%" + user.alias + "%";
                }
                if (!string.IsNullOrEmpty(user.firstName))
                {
                    firstName = "%" + user.firstName + "%";
                }
                if (!string.IsNullOrEmpty(user.lastName))
                {
                    lastName = "%" + user.lastName + "%";
                }
                if (!string.IsNullOrEmpty(user.userNo))
                {
                    userNo = "%" + user.userNo + "%";
                }

                var ls = PWDB.PW_User.Where(p => System.Data.Linq.SqlClient.SqlMethods.Like(p.showRule, showRule) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.email, email) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.phone, phone) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.sex, sex) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.alias, alias) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.firstName, firstName) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.lastName, lastName) ||
                                                 System.Data.Linq.SqlClient.SqlMethods.Like(p.userNo, userNo)
                                             );
                return ls.ToList();

            }

            //[3]
            if (type == 2)
            {
                if (user.showRule != null)
                {
                    return PWDB.PW_User.Where(p => p.showRule == user.showRule).ToList();
                }
                if (user.email != null)
                {
                    return PWDB.PW_User.Where(p => p.email == user.email).ToList();
                }
                if (user.phone != null)
                {
                    return PWDB.PW_User.Where(p => p.phone == user.phone).ToList();
                }
                if (user.birthDate != null)
                {
                    return PWDB.PW_User.Where(p => p.birthDate == user.birthDate).ToList();
                }
                if (user.sex != null)
                {
                    return PWDB.PW_User.Where(p => p.sex == user.sex).ToList();
                }
                if (user.alias != null)
                {
                    return PWDB.PW_User.Where(p => p.alias == user.alias).ToList();
                }
                if (user.firstName != null)
                {
                    return PWDB.PW_User.Where(p => p.firstName == user.firstName).ToList();
                }
                if (user.lastName != null)
                {
                    return PWDB.PW_User.Where(p => p.lastName == user.lastName).ToList();
                }
                if (user.userNo != null)
                {
                    return PWDB.PW_User.Where(p => p.userNo == user.userNo).ToList();
                }
            }

            return PWDB.PW_User.Where(p => false).ToList();
        }

        public List<PW_User> getUsersByCondition(string pid, string userNo, string deptNo, string dutyNo)
        {
            Expression<Func<PW_User, bool>> expr = PredicateExtensions.True<PW_User>();
            if (!string.IsNullOrEmpty(userNo))
                expr = expr.And(u => u.userNo.Equals(userNo));
            if (!string.IsNullOrEmpty(deptNo))
                expr = expr.And(u => u.PW_DepartmentDutyUserRelation.Any(p => p.departmentNo.Equals(deptNo)));
            if (!string.IsNullOrEmpty(dutyNo))
                expr = expr.And(u => u.PW_DepartmentDutyUserRelation.Any(p => p.dutyNo.Equals(dutyNo)));
            return PWDB.PW_User.Where(expr).ToList();
        }
        //-

        //[3.15	删除人员]
        public int? removeUser(string pid, string userNo)
        {
            if (checkKey(userNo)) { return -1000; }

            //[1]
            if (string.IsNullOrEmpty(userNo))
            {
                return 1;
            }

            //[2]
            if (PWDB.PW_User.Count(p => p.userNo == userNo) == 1)
            {

                var pw_user = PWDB.PW_User.Single(p => p.userNo == userNo);

                PWDB.PW_User.DeleteOnSubmit(pw_user);

                return submit();
            }
            else
            {
                return 2;
            }

        }

        public PW_Menu getMenuByNo(string menuNo)
        {
            return PWDB.PW_Menu.SingleOrDefault(m => m.menuNo == menuNo);
        }

        //查询所有菜单
        public List<PW_Menu> getAllMenu()
        {
            List<PW_Menu> menus = new List<PW_Menu>();
            PWDB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, menus);
            menus = PWDB.PW_Menu.OrderBy(m => m.orderIndex).ToList();
            return menus;
        }

        //查询所有根节点菜单
        public List<PW_Menu> getRootMenu(string userNo)
        {
            if (string.IsNullOrEmpty(userNo) || userNo.Equals("system"))
            {
                return PWDB.PW_Menu.Where(a => a.menuType == 10).ToList().Where(a => a.parentMenu == null).OrderBy(a => a.orderIndex).ToList();
            }
            else
            {
                var dutys = PWDB.PW_Duty.Where(m => m.PW_DepartmentDutyUserRelation.Any(dur => dur.userNo == userNo)).ToList();
                if (dutys.Count == 0)
                {
                    return null;
                }
                List<PW_Menu> menus = new List<PW_Menu>();
                foreach (var duty in dutys)
                {
                    var menu = PWDB.PW_Menu.Where(a => a.menuType == 10).ToList().Where(a => a.parentMenu == null)
                        .Where(m => m.PW_MenuDutyRelation.Any(dr => dr.dutyID.ToString() == duty.ID.ToString())).OrderBy(m => m.orderIndex).ToList();
                    menus.AddRange(menu);
                }
                return menus.Distinct().OrderBy(a => a.orderIndex).ToList();
            }
        }

        public List<PW_Menu> getChildMenuByParentMenu(string parentMenu, string userNo)
        {
            if (string.IsNullOrEmpty(parentMenu))
                return null;
            if (string.IsNullOrEmpty(userNo) || userNo.Equals("system"))
            {
                return PWDB.PW_Menu.Where(a => a.parentMenu == parentMenu && a.menuType != 30).ToList();
            }
            else
            {
                var dutys = PWDB.PW_Duty.Where(m => m.PW_DepartmentDutyUserRelation.Any(dur => dur.userNo == userNo)).ToList();
                if (dutys.Count == 0)
                {
                    return null;
                }
                List<PW_Menu> menus = new List<PW_Menu>();
                foreach (var duty in dutys)
                {
                    var menu = PWDB.PW_Menu.Where(a => a.parentMenu == parentMenu && a.menuType != 30)
                        .Where(m => m.PW_MenuDutyRelation.Any(dr => dr.dutyID.ToString() == duty.ID.ToString())).OrderBy(m => m.orderIndex).ToList();
                    menus.AddRange(menu);
                }
                return menus.Distinct().OrderBy(a => a.orderIndex).ToList();
            }

        }

        public List<PW_Menu> getMenuByForm(string formName)
        {
            if (string.IsNullOrEmpty(formName))
            {
                return null;
            }
            return PWDB.PW_Menu.Where(a => a.formName == formName).ToList();
        }

        public List<PW_Menu> getMenuByUserNo(string userNo)
        {
            if (string.IsNullOrEmpty(userNo))
            {
                return null;
            }
            var dutys = PWDB.PW_Duty.Where(m => m.PW_DepartmentDutyUserRelation.Any(dur => dur.userNo == userNo)).ToList();
            if (dutys.Count == 0)
            {
                return null;
            }
            List<PW_Menu> menus = new List<PW_Menu>();
            foreach (var duty in dutys)
            {
                var menu = PWDB.PW_Menu.Where(m => m.PW_MenuDutyRelation.Any(dr => dr.dutyID.ToString() == duty.ID.ToString())).OrderBy(m => m.orderIndex).ToList();
                menus.AddRange(menu);
            }
            return menus.Distinct().OrderBy(a => a.orderIndex).ToList();
        }

        public int? insertMenu(string pid, PW_Menu menu)
        {
            //[1]
            if (menu == null)
            {
                return 1;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(menu.menuNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(menu.menuName))
            {
                return 3;
            }
            //-

            //[4]
            menu.ID = Guid.NewGuid();
            //-

            //[10]
            PWDB.PW_Menu.InsertOnSubmit(menu);
            this.submit();

            return null;

        }

        public int? updateMenu(string pid, PW_Menu menu)
        {
            //[1]
            if (menu == null)
            {
                return 1;
            }
            //-

            //[2]
            if (string.IsNullOrEmpty(menu.menuNo))
            {
                return 2;
            }
            //-

            //[3]
            if (string.IsNullOrEmpty(menu.menuName))
            {
                return 3;
            }
            //-

            if (PWDB.PW_Menu.Count(p => p.ID == menu.ID) == 1)
            {
                var pw_menu = PWDB.PW_Menu.Single(p => p.ID == menu.ID);
                pw_menu.menuName = menu.menuName;
                pw_menu.menuNo = menu.menuNo;
                pw_menu.menuType = menu.menuType;
                pw_menu.parentMenu = menu.parentMenu;
                pw_menu.exeFile = menu.exeFile;
                pw_menu.formName = menu.formName;
                pw_menu.orderIndex = menu.orderIndex;
                pw_menu.imageName = menu.imageName;
                return submit();
            }
            else
            {
                return 4;
            }
        }

        public int? insertMenuDuty(Guid menuId, List<PW_MenuDutyRelation> menuDutyRelations)
        {
            if (menuDutyRelations == null)
            {
                return 1;
            }
            if (menuDutyRelations.Count == 0)
            {
                List<PW_MenuDutyRelation> deleteList = PWDB.PW_MenuDutyRelation.Where(a => a.menuID == menuId).ToList();
                PWDB.PW_MenuDutyRelation.DeleteAllOnSubmit<PW_MenuDutyRelation>(deleteList);
            }
            else
            {
                List<PW_MenuDutyRelation> deleteList = PWDB.PW_MenuDutyRelation.Where(a => a.menuID == menuDutyRelations[0].menuID).ToList();
                PWDB.PW_MenuDutyRelation.DeleteAllOnSubmit<PW_MenuDutyRelation>(deleteList);
                PWDB.PW_MenuDutyRelation.InsertAllOnSubmit(menuDutyRelations);
            }
            return submit();
        }

        public int? delMenuAndPower(Guid menuId)
        {
            if (menuId == null)
                return 1;
            var menu = PWDB.PW_Menu.SingleOrDefault(a => a.ID == menuId);
            if (menu == null)
                return 2;
            var menuDuty = PWDB.PW_MenuDutyRelation.Where(a => a.menuID == menuId);
            PWDB.PW_MenuDutyRelation.DeleteAllOnSubmit<PW_MenuDutyRelation>(menuDuty);
            PWDB.PW_Menu.DeleteOnSubmit(menu);
            return submit();
        }

        public List<MenuDto> getMenuSelectedAllByDuty(PW_Duty duty)
        {
            var menus = PWDB.PW_Menu.Where(a => a.menuType != 30).OrderBy(a => a.orderIndex).ToList();
            List<MenuDto> menusDto = new List<MenuDto>();
            List<PW_MenuDutyRelation> rels = PWDB.PW_MenuDutyRelation.Where(a => a.dutyID == duty.ID).ToList();
            foreach (var menu in menus)
            {
                Mapper.CreateMap<PW_Menu, MenuDto>();
                var menuDto = AutoMapper.Mapper.Map<PW_Menu, MenuDto>(menu);
                if (rels != null)
                {
                    bool isExist = rels.Exists(a => a.menuID == menu.ID && a.dutyID == duty.ID);
                    if (isExist)
                    {
                        menuDto.selectedFlag = 1;
                    }
                }
                menusDto.Add(menuDto);
            }
            return menusDto;
        }

        public int? saveMenuDutyRelForDuty(PW_Duty duty, List<PW_MenuDutyRelation> rels)
        {
            List<PW_MenuDutyRelation> his = PWDB.PW_MenuDutyRelation.Where(a => a.dutyID == duty.ID).ToList();
            if (his != null)
            {
                PWDB.PW_MenuDutyRelation.DeleteAllOnSubmit<PW_MenuDutyRelation>(his);
            }
            PWDB.PW_MenuDutyRelation.InsertAllOnSubmit<PW_MenuDutyRelation>(rels);
            return submit();
        }

        public List<PW_Object> getDutyObjRelsByUserNo(string userNo)
        {
            var query = from udr in PWDB.PW_DepartmentDutyUserRelation
                        join duty in PWDB.PW_Duty on udr.dutyNo equals duty.dutyNo
                        join odr in PWDB.PW_ObjectDutyRelation on duty.ID equals odr.dutyID
                        join obj in PWDB.PW_Object on odr.objectID equals obj.ID
                        where udr.userNo == userNo
                        select obj;
            return query.ToList();
        }

        public List<PW_Menu> getMenuByType(int menuType)
        {
            return PWDB.PW_Menu.Where(a => a.menuType == menuType).ToList();
        }

        public int? createMenuObjects(List<MenuObjectDto> obj)
        {
            List<PW_Object> objs = new List<PW_Object>();
            foreach (var dto in obj)
            {
                Mapper.CreateMap<MenuObjectDto, PW_Object>();
                var o = Mapper.Map<MenuObjectDto, PW_Object>(dto);
                objs.Add(o);
            }
            PWDB.PW_Object.InsertAllOnSubmit(objs);
            return submit();
        }

        public List<MenuObjectDto> getMenuObjectAll()
        {
            try
            {
                var query = from obj in PWDB.PW_Object
                            join menu in PWDB.PW_Menu on obj.menuID equals menu.ID
                            orderby menu.orderIndex
                            select new { obj.ID, obj.menuID, obj.objectName, obj.objectCode, obj.remark, menu.menuName };
                if (query.Count() == 0)
                    return null;
                List<MenuObjectDto> r = new List<MenuObjectDto>();
                foreach (var q in query)
                {
                    MenuObjectDto d = new MenuObjectDto();
                    d.ID = q.ID;
                    d.menuID = q.menuID;
                    d.menuName = q.menuName;
                    d.objectCode = q.objectCode;
                    d.objectName = q.objectName;
                    d.remark = q.remark;
                    r.Add(d);
                }
                return r;
            }
            catch (Exception ex)
            {
                MyDebug.write(ex.Message);
                return null;
            }

        }

        public List<MenuObjectDto> getMenuObjectByMenuId(Guid meunId)
        {
            if (meunId == null)
                return null;
            var objs = PWDB.PW_Object.Where(a => a.menuID == meunId).ToList();
            if (objs == null && objs.Count == 0)
                return null;

            var query = from obj in PWDB.PW_Object
                        join menu in PWDB.PW_Menu on obj.menuID equals menu.ID
                        where menu.ID == meunId
                        select new { obj.ID, obj.menuID, obj.objectName, obj.objectCode, obj.remark, menu.menuName };
            List<MenuObjectDto> r = new List<MenuObjectDto>();
            foreach (var q in query)
            {
                MenuObjectDto d = new MenuObjectDto();
                d.ID = q.ID;
                d.menuID = q.menuID;
                d.menuName = q.menuName;
                d.objectCode = q.objectCode;
                d.objectName = q.objectName;
                d.remark = q.remark;
                r.Add(d);
            }
            return r;
        }

        public int? addMenuObject(MenuObjectDto objDto)
        {
            if (objDto == null)
                return 1;
            Mapper.CreateMap<MenuObjectDto, PW_Object>();
            var obj = Mapper.Map<MenuObjectDto, PW_Object>(objDto);
            PWDB.PW_Object.InsertOnSubmit(obj);
            return submit();
        }

        public int? updateMenuObject(MenuObjectDto objDto)
        {
            if (objDto == null)
                return 1;
            var u = PWDB.PW_Object.SingleOrDefault(a => a.ID == objDto.ID);
            u.menuID = objDto.menuID;
            u.objectCode = objDto.objectCode;
            u.objectName = objDto.objectName;
            u.remark = objDto.remark;
            return submit();
        }

        public int? delMenuObject(MenuObjectDto objDto)
        {
            var u = PWDB.PW_Object.SingleOrDefault(a => a.ID == objDto.ID);
            PWDB.PW_ObjectDutyRelation.DeleteAllOnSubmit(u.PW_ObjectDutyRelation);
            PWDB.PW_Object.DeleteOnSubmit(u);
            return submit();
        }


        public int? saveDutyPower(PW_Duty duty, List<PW_MenuDutyRelation> mrels, List<PW_ObjectDutyRelation> addOrels, List<PW_ObjectDutyRelation> delOrels)
        {
            List<PW_MenuDutyRelation> his = PWDB.PW_MenuDutyRelation.Where(a => a.dutyID == duty.ID).ToList();
            if (his != null)
            {
                PWDB.PW_MenuDutyRelation.DeleteAllOnSubmit<PW_MenuDutyRelation>(his);
            }
            PWDB.PW_MenuDutyRelation.InsertAllOnSubmit<PW_MenuDutyRelation>(mrels);

            PWDB.PW_ObjectDutyRelation.DeleteAllOnSubmit<PW_ObjectDutyRelation>(delOrels);
            PWDB.PW_ObjectDutyRelation.InsertAllOnSubmit<PW_ObjectDutyRelation>(addOrels);
            return submit();
        }

        public List<MenuObjectDto> getMenuObject(Guid menuId, Guid dutyId)
        {
            var query = from obj in PWDB.PW_Object
                        join menu in PWDB.PW_Menu on obj.menuID equals menu.ID
                        join uobj in
                            (from odr in PWDB.PW_ObjectDutyRelation
                             join obj in PWDB.PW_Object on odr.objectID equals obj.ID
                             where odr.dutyID == dutyId
                             select obj).Distinct() on obj.ID equals uobj.ID into temp
                        from rel in temp.DefaultIfEmpty()
                        where menu.ID == menuId
                        orderby obj.objectCode
                        select new { obj.ID, obj.menuID, obj.objectName, obj.objectCode, obj.remark, menu.menuName, selectedFlag = (rel.objectName == null ? 0 : 1) };
            List<MenuObjectDto> r = new List<MenuObjectDto>();
            foreach (var q in query)
            {
                MenuObjectDto d = new MenuObjectDto();
                d.ID = q.ID;
                d.menuID = q.menuID;
                d.menuName = q.menuName;
                d.objectCode = q.objectCode;
                d.objectName = q.objectName;
                d.remark = q.remark;
                d.selectedFlag = q.selectedFlag;
                r.Add(d);
            }
            return r;
        }

        public List<PW_Object> getMenuObjectByUserNo(Guid menuId, string userNo)
        {
            var query = from obj in PWDB.PW_Object
                        join menu in PWDB.PW_Menu on obj.menuID equals menu.ID
                        join uobj in
                            (from odr in PWDB.PW_ObjectDutyRelation
                             join obj in PWDB.PW_Object on odr.objectID equals obj.ID
                             join dur in PWDB.PW_DepartmentDutyUserRelation on odr.PW_Duty.dutyNo equals dur.dutyNo
                             where dur.userNo == userNo
                             select obj).Distinct() on obj.ID equals uobj.ID
                        where menu.ID == menuId
                        orderby obj.objectCode
                        select obj;
            return query.ToList();
        }


        public List<PW_Menu> getFormAll()
        {
            try
            {
                return PWDB.PW_Menu.Where(a => a.menuType == 30).OrderBy(a => a.menuNo).ToList();
            }
            catch (Exception ex)
            {
                MyDebug.write(ex.Message);
                return null;

            }
        }


    }


}